package io.rainforest.rss.web.rssmain;

import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import io.rainforest.rss.base.exception.ServiceException;
import io.rainforest.rss.base.request.R;
import io.rainforest.rss.dao.po.RssFollow;
import io.rainforest.rss.dao.po.RssMain;
import io.rainforest.rss.service.rssfollow.FollowService;
import io.rainforest.rss.service.rssmain.RssMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.component.rss.RssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author r0ad
 */
@RestController
@Api(tags = "Rss")
@RequestMapping("/v1/rss/")
@Slf4j
public class RssInitController {

    @Autowired
    private RssMainService rssMainService;
    @Autowired
    private FollowService followService;

    @ApiOperation(value = "通过uri读取Rss最新信息")
    @PostMapping("readOne")
    @ResponseBody
    public R<SyndFeed> readOne(String uri) {
        try {
            SyndFeed feed = RssUtils.createFeed(uri);
            return R.ok(feed);
        } catch (Exception e) {
            log.error("查询rss日志信息异常 {}",e);
            throw new ServiceException(e.getMessage(),3000);
        }
    }

    @ApiOperation(value = "通过数据库读取Rss最新信息")
    @PostMapping("readHis")
    @ResponseBody
    public R<List<RssMain>> readHis(String rssId,String title) {
        try {
            List<RssMain> rssMains = rssMainService.queryList(rssId,title);
            return R.ok(rssMains);
        } catch (Exception e) {
            log.error("查询rss日志信息异常 {}",e);
            throw new ServiceException(e.getMessage(),3000);
        }
    }

    @ApiOperation(value = "读取Rss的最新数据并且增量入库")
    @PostMapping("addFetch")
    @ResponseBody
    public R addFetch(String rssId) {
        try {
            RssFollow rssFollow = followService.queryOne(rssId);
            SyndFeed feed = RssUtils.createFeed(rssFollow.getRssLink());
            List<SyndEntry> entries = feed.getEntries();
            if(!CollectionUtils.isEmpty(entries)){
                Collections.reverse(entries);
                entries.forEach(item->{
                    rssMainService.addEntryAndCheck(item,rssId);
                });
            }
            return R.ok("更新成功");
        } catch (Exception e) {
            log.error("查询rss日志信息异常 {}",e);
            throw new ServiceException(e.getMessage(),3000);
        }
    }

    @ApiOperation(value = "更新Rss订阅信息")
    @PostMapping("updateRss")
    @ResponseBody
    public R updateRss(@RequestBody RssMain main) {
        if(ObjectUtils.isEmpty(main.getUuid())){
            throw new ServiceException("uuid不能为空",3002);
        }
        // 更新信息
        rssMainService.updateSelective(main);
        return R.ok("更新成功");
    }

}
